<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class ProductComponent
{
	function ProductComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$product_id = (int)$params['productId'];
		$language_id = (int)$params['languageId'];
		$isAdministrator = (boolean)$params['isAdministrator'];

		if ($product_id > 0)
		{
			$cache_id = $language_id . '_' . $product_id;
			if ($isAdministrator) $cache_id .= '_true';
			else $cache_id .= '_false';

			$json = read_cache($cache_id, $handle);
			if ($json)
			{
				if ($json == 'null') return null;
				else return json_decode($json, true);
			}

			$subParams['handle'] = $handle;
			$subParams['languageId'] = $language_id;
			$subParams['productId'] = $product_id;
			$subParams['max_rows'] = 1;
			$subParams['return_details'] = true;

			if ($isAdministrator) $subParams['include_all_status'] = true;

			require_once('includes/components/ProductsSearchSubComponent.php');
			$subComponent = new ProductsSearchSubComponent();
			$value = $subComponent->query($subParams);

			$value{'ProductComponentMetaData'}{'prev_next_bar_location'}= (int)PREV_NEXT_BAR_LOCATION;
					
			if ($value != null) $json = json_encode($value);
			write_cache($json, $cache_id, $handle);

			return $value;
		} 
		else
		{
			return null;
		}
	}
}
?>
